package com.example.vv.model.setupItem.base.presenter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.widget.TextView;

import com.example.vv.R;
import com.example.vv.adapter.BaseRecyclerViewAdapter;
import com.example.vv.adapter.RecyclerViewHolder;
import com.example.vv.fragmentWork.entity.NaviEntity;
import com.example.vv.fragmentWork.entity.TreeEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.model.accountDetails.AIDActivity;
import com.example.vv.model.setupItem.base.contract.SetupItemContract;
import com.example.vv.model.setupItem.base.model.SetupItemModel;
import com.example.vv.model.setupItemDetails.SIDActivity;
import com.example.vv.res.net.BaseResponse;
import com.example.vv.res.net.NetUrl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Random;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class SetupItemPresenter extends MvpPresenter<SetupItemContract.View,SetupItemContract.Model> implements SetupItemContract.Presenter{
    List<NaviEntity.ArticlesBean> articles;
    private Paint mSearchHistoryPaint;
    private int mPaddingSize;

    @Override
    public void attach(SetupItemContract.View view) {
        super.attach(view);
        model = new SetupItemModel();
    }

   /* @Override
    public void setTree() {
        model.GET_API(NetUrl.GET_API_TREE)
                .map(new Function<BaseResponse, List<TreeEntity>>() {
                    @Override
                    public List<TreeEntity> apply(BaseResponse baseResponse) throws Exception {
                        return new Gson().fromJson(baseResponse.getData(),new TypeToken<List<TreeEntity>>(){}.getType());
                    }
                })
                .subscribe(new Consumer<List<TreeEntity>>() {
                    @Override
                    public void accept(List<TreeEntity> treeEntities) throws Exception {
                        view().getTree(treeEntities);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }*/

    @Override
    public void setNative() {
        model.GET_API(NetUrl.GET_API_NAVI)
                .map(new Function<BaseResponse, List<NaviEntity>>() {
                    @Override
                    public List<NaviEntity> apply(BaseResponse baseResponse) throws Exception {
                        return new Gson().fromJson(baseResponse.getData(),new TypeToken<List<NaviEntity>>(){}.getType());
                    }
                })
                .subscribe(new Consumer<List<NaviEntity>>() {
                    @Override
                    public void accept(List<NaviEntity> naviEntities) throws Exception {
                        view().getNative(naviEntities);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }

   /* @Override
    public BaseRecyclerViewAdapter<TreeEntity> setTreeAdapter(List<TreeEntity> list, Activity activity) {
        BaseRecyclerViewAdapter<TreeEntity> adapter = new BaseRecyclerViewAdapter<TreeEntity>(activity, list, R.layout.item_host_list) {
            @Override
            protected void onBindData(RecyclerViewHolder holder, TreeEntity bean, int position) {
                TextView textView = (TextView) holder.getView(R.id.item_flow_title);
                textView.setText(bean.getName());
                RecyclerView recyclerView = (RecyclerView) holder.getView(R.id.host_re_view);
                children = bean.getChildren();
                *//**
                 * 嵌套
                 *//*
                BaseRecyclerViewAdapter<TreeEntity.ChildrenBean> adapter1 = new BaseRecyclerViewAdapter<TreeEntity.ChildrenBean>(activity,children,R.layout.item_test) {
                    @Override
                    protected void onBindData(RecyclerViewHolder holder, TreeEntity.ChildrenBean bean, int position) {
                        TextView view = (TextView) holder.getView(R.id.t1);
                        view.setText(bean.getName());
                        view.setOnClickListener((v)->{

                        });
                    }
                };
                recyclerView.setHasFixedSize(true);
                mSearchHistoryPaint = new Paint();//画笔用来计算文字的宽度
                //设置文字的大小，注意布局文件里写的是sp，这里需要进行转换成对应的px，我这里就不写了
                mSearchHistoryPaint.setTextSize(60);
                //textview的大小，包括文字大小和左右内边距，所以这个值表示内边距大小，同样注意转换
                mPaddingSize = 100;
                //获取屏幕的宽度，我这里rccyclerview正好是屏幕的宽度，否则应替换为rec的宽度
                final int width = activity.getWindowManager().getDefaultDisplay().getWidth();
                //这里讲网格的数量设置为屏幕的宽度，网格无限小
                GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, width);
                //设置每个文字占据的网格数
                gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        //就是这里 需要测量文字的宽度，加上左右内边距
                        int textWidth = (int) mSearchHistoryPaint.measureText(children.get(position).getName()) + mPaddingSize;
                        //如果文字的宽度超过屏幕的宽度，那么我们就设置为屏幕宽度
                        return textWidth > width ? width : textWidth;
                    }
                });
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(adapter1);
            }
        };


        return adapter;
    }*/

    @Override
    public BaseRecyclerViewAdapter<NaviEntity> setNativeAdapter(List<NaviEntity> list, Activity activity) {
        BaseRecyclerViewAdapter<NaviEntity> adapter = new BaseRecyclerViewAdapter<NaviEntity>(activity, list, R.layout.item_host_list) {
            @Override
            protected void onBindData(RecyclerViewHolder holder, NaviEntity bean, int position) {
                TextView textView = (TextView) holder.getView(R.id.item_flow_title);
                textView.setText(bean.getName());
                RecyclerView recyclerView = (RecyclerView) holder.getView(R.id.host_re_view);
                articles = bean.getArticles();
                /**
                 * 嵌套
                 */
                BaseRecyclerViewAdapter<NaviEntity.ArticlesBean> adapter1 = new BaseRecyclerViewAdapter<NaviEntity.ArticlesBean>(activity,articles, R.layout.item_test) {
                    @Override
                    protected void onBindData(RecyclerViewHolder holder, NaviEntity.ArticlesBean bean, int position) {
                        TextView view = (TextView) holder.getView(R.id.t1);
                        view.setText(bean.getTitle());
                        Random random = new Random();
                        int y = random.nextInt(255);
                        int z = random.nextInt(255);
                        int h = random.nextInt(255);
                        view.setTextColor(Color.argb(255, y, z, h));

                        view.setOnClickListener((v)->{
                            EventBus.getDefault().postSticky(bean);
                            Intent intent  = new Intent(activity, SIDActivity.class);
                            activity.startActivity(intent);
                        });
                    }
                };

                recyclerView.setHasFixedSize(true);
                mSearchHistoryPaint = new Paint();//画笔用来计算文字的宽度
                //设置文字的大小，注意布局文件里写的是sp，这里需要进行转换成对应的px，我这里就不写了
                mSearchHistoryPaint.setTextSize(70);
                //textview的大小，包括文字大小和左右内边距，所以这个值表示内边距大小，同样注意转换
                mPaddingSize = 100;
                //获取屏幕的宽度，我这里rccyclerview正好是屏幕的宽度，否则应替换为rec的宽度
                final int width = activity.getWindowManager().getDefaultDisplay().getWidth();
                //这里讲网格的数量设置为屏幕的宽度，网格无限小
                GridLayoutManager gridLayoutManager = new GridLayoutManager(activity, width);
                //设置每个文字占据的网格数
                gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        //就是这里 需要测量文字的宽度，加上左右内边距
                        int textWidth = (int) mSearchHistoryPaint.measureText(articles.get(position).getChapterName()) + mPaddingSize;
                        //如果文字的宽度超过屏幕的宽度，那么我们就设置为屏幕宽度
                        return textWidth > width ? width : textWidth;
                    }
                });
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(adapter1);
            }
        };
        return adapter;
    }

}
